package com.ndp.fb.walle.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.model.business.vo.ectemplate.EcElementFilterParam;
import com.ndp.fb.rdb.api.EcElementCategoryRdbService;
import com.ndp.fb.rdb.api.EcTemplateRdbService;
import com.ndp.fb.rdb.model.EcElementCategory;
import com.ndp.fb.rdb.model.EcTemplate;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.rdb.model.ecelement.EcElementAttach;
import com.ndp.fb.walle.business.EcTemplateDetailService;
import com.ndp.fb.walle.model.vo.ectemplate.param.EcTemplatePreviewElementParam;
import com.ndp.fb.walle.model.vo.ectemplate.param.EcTemplateUpdateDesignParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by evan on 2016/7/4.
 */
@RestController
@RequestMapping("/ectemplatedetail")
public class EcTemplateDetailController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    EcTemplateDetailService ecTemplateDetailService;

    @Reference
    EcTemplateRdbService ecTemplateRdbService;

    /**
     * 获取所有元素类型 所属大类列表
     * like 快递/送达 、 免邮包邮 、 上新
     *
     * 需要除过 一些特性元素
     */
    @RequestMapping("getElementBigCategory")
    public List<EcElementCategory> getElementBigCategory(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        return ecTemplateDetailService.findBigCatagory(user.getLanguage());
    }

    @RequestMapping("getElementSmallCategory")
    public List<EcElementCategory> getElementSmallCategory(@RequestBody EcElementFilterParam param, HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        return ecTemplateDetailService.getElementSmallCategory(user.getLanguage(), param.getElementType());
    }

    /**
     * 获取模板下的设计元素  主体背景和主题装饰
     */
    @RequestMapping("getDesignEleByTmpl")
    public Map<String, EcElementAttach> getDesignEleByTmpl(@RequestBody EcTemplatePreviewElementParam param, HttpServletRequest req) {
        Map<String, EcElementAttach> data = new HashMap<>();
        User user = (User) req.getSession().getAttribute("user");
        data.put("bg", ecTemplateDetailService.getBgDesignEleByTmpl(param.getTemplateId(), user.getOrgId()));
        data.put("bk", ecTemplateDetailService.getBkDesignEleByTmpl(param.getTemplateId(), user.getOrgId()));
        return data;
    }

    /**
     * 更新模板的 设计元素编码
     */
    @RequestMapping("updateDesignField")
    public EcTemplate updateDesignField(@RequestBody EcTemplateUpdateDesignParam param) {
        Integer a = 1;
        Integer c = 2;
        Integer b = 3;
        if(a.equals(param.getAttachElementType()) || c.equals(param.getAttachElementType()))
            ecTemplateDetailService.updateTmplBgStyle(param.getTemplateId(), param.getTemplateName(), param.getDesignCode());
        else if(b.equals(param.getAttachElementType()))
            ecTemplateDetailService.updateTmplBk(param.getTemplateId(), param.getTemplateName(), param.getDesignCode().toUpperCase());

        return ecTemplateRdbService.findById(param.getTemplateId());
    }

}
